// ActionScript file
import components.GroupItem;

import mx.controls.Alert;
import mx.events.MenuEvent;

import org.flexlancer.Logger.Logger;
import org.flexlancer.enums.AppVariables;
import org.flexlancer.events.AssetEvent;
import org.flexlancer.managers.AssetManager;
import org.flexlancer.managers.SelectionManager;
import org.flexlancer.utils.GeneralUtils;
import org.flexlancer.utils.StringUtils;


/*
=========================================================
=========================================================
		VARIABLES
=========================================================
=========================================================
*/

private var xmlData:XML;

[Bindable]
private var lblUser_text:String;

[Bindable]
private var lblVersion_text:String = "3.2";

private var assetsManager:AssetManager = new AssetManager;
/*
=========================================================
=========================================================
		CONFIG FILE SCRIPTS
=========================================================
=========================================================
*/

private function OnCreationComplete():void{
	try{
		GeneralUtils.parentComponent = this;
		this.assetsManager.addEventListener(AssetEvent.ASSETS_LOADED,OnAssetsLoaded);
		this.LoadConfigFile();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function LoadConfigFile():void{
	try{
		var ldrConfig:URLLoader = new URLLoader();
		
		ldrConfig.addEventListener(Event.COMPLETE,OnConfigFileLoad);
		ldrConfig.addEventListener(IOErrorEvent.IO_ERROR,OnConfigFileError);
		
		ldrConfig.load(new URLRequest("config.xml"));
		GeneralUtils.Show("Config.xml");
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnConfigFileLoad(evt:Event):void{
	try{
		var xmlData:XML = new XML(evt.target.data);
		AppVariables.SERVER_URL = String(xmlData.@ServerURL);
		if ( xmlData != null ){
			if ( xmlData.URLS != null ){
				
				if ( xmlData.URLS.GetElements != null ){
					AppVariables.URL_GET_ELEMENTS = AppVariables.SERVER_URL + xmlData.URLS.GetElements.toString();
				}
				
				if ( xmlData.URLS.GetThumbnail != null ){
					AppVariables.URL_GET_THUMBNAIL = AppVariables.SERVER_URL + xmlData.URLS.GetThumbnail.toString();
				}
				
				
				this.assetsManager.LoadAssets(xmlData.ASSETS.children());
				//this.LoadURLs();
				GeneralUtils.Hide();
				return;
			}
		}

		GeneralUtils.Hide();
		Alert.show("Invalid or Bad format config.xml");
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnConfigFileError(evt:Event):void{
	try{
		GeneralUtils.Hide();
		Alert.show("Config.xml is missing or not accessable");
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAssetsLoaded(evt:AssetEvent):void{
	try{
		this.LoadURLs();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
/*
=========================================================
=========================================================
		DATA FILE SCRIPTS
=========================================================
=========================================================
*/

private function LoadURLs():void{
	try{
		var ldrData:URLLoader = new URLLoader();
		
		ldrData.addEventListener(Event.COMPLETE,OnDataFileLoad);
		ldrData.addEventListener(IOErrorEvent.IO_ERROR,OnDataFileError);
		
		AppVariables.APP_DATA = Application.application.parameters.datafile;
		AppVariables.APP_ELEMENTS =  Application.application.parameters.elementsfile;
		AppVariables.APP_USER = Application.application.parameters.user;
		AppVariables.APP_CASE_FILE_ID = Application.application.parameters.casefileid;
		
		this.lblUser_text = AppVariables.APP_USER;
		 
		if ( ! StringUtils.IsNullOrEmpty(AppVariables.APP_DATA) ){
			ldrData.load(new URLRequest(AppVariables.APP_DATA));
			GeneralUtils.Show("Loading data file.");
		}else{
			Alert.show("Data file is missing. application starts in NEW mode");
			this.etElements.LoadData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnDataFileLoad(evt:Event):void{
	try{			
		this.xmlData = XML(evt.target.data);
		GeneralUtils.Hide();
	}catch(err:Error){
		Logger.LogE(err);
	}//catch
}

private function OnDataFileError(evt:IOErrorEvent):void{
	try{
		Alert.show(AppVariables.APP_DATA + " file not found","Data file not found.");
		GeneralUtils.Hide();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

/*
=========================================================
=========================================================
		MENU CONTROL SCRIPTS
=========================================================
=========================================================
*/

private function AppMenuBar_OnItemClicked(evt:MenuEvent):void{
	try{
		var menu_action:String = String(evt.item.@action);
		switch(menu_action.toUpperCase()){
			case "CREATE_GROUP":
				this.dvcMainCanvas.AddGroup(SelectionManager.SelectionRectangle);
			break;
			case "UNGROUP_GROUP":
				if ( SelectionManager.SelectedItem != null ){
					if ( SelectionManager.SelectedItem is GroupItem ){
						this.dvcMainCanvas.UnGroup(GroupItem(SelectionManager.SelectedItem));
					}
				}
			break;
			case "DELETE_GROUP":
				if ( SelectionManager.SelectedItem != null ){
					if ( SelectionManager.SelectedItem is GroupItem ){
						this.dvcMainCanvas.DeleteGroup(GroupItem(SelectionManager.SelectedItem));
					}
				}
			break;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}